package com.xxx;

import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;

/**
 * 一元通信方式 单个请求参数
 */
public class GrpcClient1 {
    public static void main(String[] args) {
        // 1.创建通信的管道
        ManagedChannel managedChannel = ManagedChannelBuilder.forAddress("localhost", 9000).usePlaintext().build();
        try {
            // 2.获取代理对象 stub
            HelloServiceGrpc.HelloServiceBlockingStub helloServiceBlockingStub = HelloServiceGrpc.newBlockingStub(managedChannel);
            // 3.完成RPC调用
            // 3.1准备参数
            HelloProto.HelloRequest.Builder builder = HelloProto.HelloRequest.newBuilder();
            builder.setName("xxx");
            // 3.2进行功能RPC调用 获取响应内容
            HelloProto.HelloResponse helloResponse = helloServiceBlockingStub.hello(builder.build());
            System.out.println("result = " + helloResponse.getResult());
        } catch (Exception e) {
            throw new RuntimeException();
        } finally {
            // 关闭通道
            managedChannel.shutdown();
        }
    }
}
